package com.big.codegen.controller;

import com.big.codegen.domain.dto.CodegenTableConfigDTO;
import com.big.codegen.domain.dto.ColumnDTO;
import com.big.codegen.service.TableCodegenService;
import com.big.common.core.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 表生成控制器
 * </p>
 *
 * @author 山归岿
 * @create 2025-06-25-21:42
 */
@Slf4j
@RestController
@RequestMapping("/table-codegen")
@RequiredArgsConstructor
@Validated
@Tag(name = "表生成", description = "表生成相关接口")
public class TableCodegenController {

    private final TableCodegenService tableCodegenService;

    @PostMapping("/generateSql")
    @Operation(summary = "生成创建表的SQL语句")
    public Result<String> generateSql(@RequestBody @Valid CodegenTableConfigDTO request) {
        String sql = tableCodegenService.generateSql(
                request.getDatasourceId(),
                request.getTableName(),
                request.getTableComment(),
                request.getColumns());
        return Result.ok(sql, "SQL生成成功");
    }

    @PostMapping("/createTable")
    @Operation(summary = "创建表")
    public Result<Boolean> createTable(@RequestBody @Valid CodegenTableConfigDTO request) {
        boolean success = tableCodegenService.createTable(request);
        return success ? Result.ok(true, "表创建成功") : Result.failed("创建失败");

    }

    @GetMapping("/tables")
    @Operation(summary = "获取数据源中的所有表名")
    public Result<List<String>> getTableNames(
            @Parameter(description = "数据源ID", required = true)
            @RequestParam @NotNull(message = "数据源ID不能为空") String datasourceId) {
        List<String> tableNames = tableCodegenService.getTableNames(datasourceId);
        return Result.ok(tableNames, "获取表名列表成功");
    }

    @GetMapping("/columns")
    @Operation(summary = "获取表的字段信息")
    public Result<List<ColumnDTO>> getTableColumns(
            @Parameter(description = "数据源ID", required = true)
            @RequestParam @NotNull(message = "数据源ID不能为空") String datasourceId,
            @Parameter(description = "表名", required = true)
            @RequestParam @NotBlank(message = "表名不能为空") String tableName) {
        List<ColumnDTO> columns = tableCodegenService.getTableColumns(datasourceId, tableName);
        return Result.ok(columns, "获取表字段信息成功");
    }

    @Operation(summary = "从数据库导入多张表配置")
    @PostMapping("/import/{datasourceId}")
    public Result<Boolean> importTables(@PathVariable("datasourceId") String datasourceId, @RequestBody List<String> tables) {
        return Result.ok(tableCodegenService.importTables(datasourceId, tables));
    }
}
